<template>
    <div class="search-box">
        <div class="row">
            <div class="left">
                <el-icon>
                    <Search />
                </el-icon>
                <span>筛选搜索</span>
            </div>
            <div class="right">
                <el-button @click="queryReset">重置</el-button>
                <el-button type="primary" @click="pageChangeHandle">查询搜索</el-button>
            </div>
        </div>
        <div class="row">
            <div>
                <span>输入搜索:</span>
                <el-input style="max-width: 223px;min-width: 200px;" placeholder="订单编号" v-model="form.id"></el-input>
            </div>
            <div>
                <span>处理状态：</span>
                <el-cascader placeholder="全部" v-model="status" :options="options" @change="Processing" />
            </div>
            <div>
                <span>申请时间:</span>
                <el-date-picker v-model="form.createTime" type="date" placeholder="请选择日期" size="default"
                    @change="changeTime('createTime')" />
            </div>
            <div>
                <span>操作人员:</span>
                <el-input style="max-width: 223px;min-width: 200px;" placeholder="订单编号"
                    v-model="form.handleMan"></el-input>
            </div>
            <div>
                <span>处理时间:</span>
                <el-date-picker v-model="form.handleTime" type="date" placeholder="请选择日期" size="default"
                    @change="changeTime('handleTime')" />
            </div>
        </div>
    </div>
    <div class="search-box">
        <div class="row_tow">
            <div class="left">
                <el-icon>
                    <Tickets />
                </el-icon>
                <span>数据列表</span>
            </div>
            <!-- <div class="right">
                <el-button type="primary" @click="addUser()" v-permission="'addUser'">添加</el-button>
            </div> -->
        </div>
    </div>
    <el-table :data="list" border table-layout="auto" cell-class-name="align-c"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column label="服务单号" prop="id" />
        <el-table-column label="申请时间">
            <template #default="{ row }">
                <span>{{ formateDate(row.createTime) }}</span>
            </template>
        </el-table-column>
        <el-table-column label="用户账号" prop="memberUsername" />
        <el-table-column label="退款金额" prop="productRealPrice" />
        <el-table-column label="申请状态">
            <template #default="{ row }">
                <p>{{
                    row.status == 0 ? '待处理' : (row.status == 1 ? '退货中' : (row.status == 2 ? '已完成' : (row.status == 3 ?
                        '已拒绝' : '')))
                }}</p>
            </template>
        </el-table-column>
        <el-table-column label="处理时间" prop="adminCount">
            <template #default="{ row }">
                <span>{{ formateDate(row.handleTime) }}</span>
            </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
            <template #default="{ row }">
                <el-button size="small" @click="router.push(`/oms/returnApplyDetail?id=${row.id}`)">查看详情</el-button>
            </template>
        </el-table-column>
    </el-table>
    <div class="left">
        <el-cascader placeholder="全部" v-model="DeliveryType" :options="Delivery" />
        <el-button type="primary" style="margin-left: 10px;" @click="ConfirmTn">确定</el-button>
    </div>
    <el-config-provider :locale="zhCn">
        <div class="page">
            <el-pagination v-model:current-page="form.pageNum" v-model:page-size="form.pageSize"
                :page-sizes="[10, 20, 50, 100]" background layout="total, sizes, prev, pager, next, jumper"
                :total="total" @size-change="pageChangeHandle" @current-change="pageChangeHandle" />
        </div>
    </el-config-provider>
</template>
<script setup lang='ts'>
import { reactive, ref } from 'vue';
import { getReturnApplyApi, getReturnApplyDeleteApi } from './api';
import { formateDate } from "@/utils/index";
import { Search, Tickets } from "@element-plus/icons-vue";
import { ElConfigProvider, ElMessage, ElMessageBox } from 'element-plus'
import zhCn from 'element-plus/lib/locale/lang/zh-cn'
import { useRouter } from 'vue-router'

const list = ref<any>([])
const total = ref()
const status = ref([])
const deleteList = ref<number[]>([])
const DeliveryType = ref([])
const router = useRouter()

let form = reactive({
    pageNum: 1,
    pageSize: 10,
    id: '',
    handleMan: '',
    status: '',
    createTime: '',
    handleTime: '',
})


const changeTime = (time: string) => {
    if (time == 'createTime') form.createTime = formateDate(form.createTime).slice(0, 10)
    if (time == 'handleTime') form.handleTime = formateDate(form.handleTime).slice(0, 10)
}

interface User {
    id: number,
    createTime: string,
    memberUsername: string,
    productRealPrice: number,
    adminCount: string,
    status: number
}

const handleSelectionChange = (val: User[]) => {
    val.forEach(item => {
        list.value.find((sur: { id: number; }) => sur.id === item.id)
        deleteList.value.push(item.id)
    })
}

const Processing = () => {
    form.status = status.value[0]
}

const options = [
    { value: '0', label: '待处理', },
    { value: '1', label: '退货中', },
    { value: '2', label: '已完成', },
    { value: '3', label: '已拒绝', },
]

const Delivery = [
    { value: '0', label: '批量删除', },
]

// 重置
const queryReset = () => {
    form.pageNum = 1
    form.pageSize = 10
    form.id = ''
    form.handleMan = ''
    form.status = ''
    form.createTime = ''
    form.handleTime = ''

    pageChangeHandle()
}

const pageChangeHandle = () => {
    getReturnApplyApi(form).then(res => {
        if (res.code === 200) {
            list.value = res.data.list
            total.value = res.data.total
        }
    })
}
pageChangeHandle()

const ConfirmTn = () => {
    if (deleteList.value.length == 0) {
        ElMessage.warning('请选择要操作的订单')
        return
    }
    if (DeliveryType.value[0] == 0) {
        ElMessageBox.confirm("是否要进行批量删除操作?", "提示?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: 'warning',
        }).then(() => {
            getReturnApplyDeleteApi(deleteList.value).then(res => {
                if (res.code === 200) {
                    ElMessage.success('删除成功')
                    pageChangeHandle()
                }
            })
        })
    } else {
        ElMessage.warning('请选择执行操作')
    }
}

</script>
<style lang="less" scoped>
:deep(.cell) {
    text-align: center;
}

:deep(.el-table__cell) {
    text-align: center;
    height: 42px;
}

.search-box {
    box-shadow: 0 0 4px 2px #ccc;
    border-radius: 5px;
    padding: 10px;
    margin-bottom: 20px;

    .row {
        padding: 0 0 20px 0;
        display: flex;
        align-items: center;

        &:first-child {
            justify-content: space-between;
        }

        &:last-child {
            margin-left: 50px;
            text-align: center;
            display: flex;
            flex-wrap: wrap;
            // justify-content: space-around;

            div {
                display: flex;
                width: 350px;
                margin-bottom: 10px;

                span {
                    width: 100px;
                }
            }
        }
    }

    .left {

        span {
            margin-left: 10px;
        }

    }

    .row_tow {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
}

.left {
    margin: 20px 0;
    height: 20px;
    float: left;
}

.page {
    margin: 20px 0;
    height: 20px;

    :deep(.el-pagination) {
        float: right;

    }
}
</style>